package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AUser;
import com.ruoyi.system.domain.dto.XcxAInformationDTO;
import com.ruoyi.system.domain.vo.HgAInformationVO;
import com.ruoyi.system.domain.vo.XcxAInformationVO;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AInformationMapper;
import com.ruoyi.system.domain.AInformation;
import com.ruoyi.system.service.IAInformationService;

/**
 * 分享类型详细Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class AInformationServiceImpl implements IAInformationService 
{
    @Autowired
    private AInformationMapper aInformationMapper;

    /**
     * 查询分享类型详细
     * 
     * @param id 分享类型详细主键
     * @return 分享类型详细
     */
    @Override
    public HgAInformationVO selectAInformationById(Long id)
    {
        return aInformationMapper.selectAInformationById(id);
    }

    /**
     * 查询分享类型详细列表
     * 
     * @param aInformation 分享类型详细
     * @return 分享类型详细
     */
    @Override
    public List<AInformation> selectAInformationList(AInformation aInformation)
    {
        return aInformationMapper.selectAInformationList(aInformation);
    }

    /**
     * 新增分享类型详细
     * 
     * @param aInformation 分享类型详细
     * @return 结果
     */
    @Override
    public int insertAInformation(AInformation aInformation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        aInformation.setUserType("1");//1代表后管录入2小程序录入
        aInformation.setuId(user.getUserId());
        aInformation.setCreateTime(DateUtils.getNowDate());
        return aInformationMapper.insertAInformation(aInformation);
    }

    /**
     * 修改分享类型详细
     * 
     * @param aInformation 分享类型详细
     * @return 结果
     */
    @Override
    public int updateAInformation(AInformation aInformation)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        aInformation.setUserType("1");//1代表后管录入2小程序录入
        aInformation.setuId(user.getUserId());
        aInformation.setUpdateTime(DateUtils.getNowDate());
        return aInformationMapper.updateAInformation(aInformation);
    }

    /**
     * 批量删除分享类型详细
     * 
     * @param ids 需要删除的分享类型详细主键
     * @return 结果
     */
    @Override
    public int deleteAInformationByIds(Long[] ids)
    {

        return aInformationMapper.deleteAInformationByIds(ids);
    }

    /**
     * 删除分享类型详细信息
     * 
     * @param id 分享类型详细主键
     * @return 结果
     */
    @Override
    public int deleteAInformationById(Long id)
    {
        return aInformationMapper.deleteAInformationById(id);
    }

    @Override
    public List<XcxAInformationVO> selectAInformationXcxList(XcxAInformationDTO inforDto) {
        try {
            inforDto.setUserId(LoginUtils.getUser().getId());
            inforDto.setAgentLevel(LoginUtils.getUser().getRole());
        }catch (Exception e){
            inforDto.setUserId(0L);
            inforDto.setAgentLevel("2");
        }
        PageUtils.startPage();
        List<XcxAInformationVO> list  = aInformationMapper.selectAInformationXcxList(inforDto);
        if(list.size() > 0){
            for (int i = 0 ; i <list.size() ;i++){
               String[] str =  list.get(i).getInfoImage().split(",");
               List<String> arr = new ArrayList<String>();
               for (String s : str){
                   arr.add(s);
               }
               list.get(i).setListImg(arr);
            }
        }
        return list;
    }

    @Override
    public XcxAInformationVO selectAInformationXcxByinfor(XcxAInformationDTO inforDto) {
        inforDto.setUserId(LoginUtils.getUser().getId());
        inforDto.setAgentLevel(LoginUtils.getUser().getRole());
        List<XcxAInformationVO> list  = aInformationMapper.selectAInformationXcxList(inforDto);
        if(list.size() > 0){
            for (int i = 0 ; i < list.size() ;i++){
                String[] str =  list.get(i).getInfoImage().split(",");
                List<String> arr = new ArrayList<String>();
                for (String s : str){
                    arr.add(s);
                }
                list.get(i).setListImg(arr);
            }
        }
        return list.get(0);
    }

    @Override
    public int addXcxInfo(AInformation inforDto) {
        AUser user = LoginUtils.getUser();
        if(user.getIsExamine().equals("0")){
            inforDto.setIsState("1");
            inforDto.setIsShow(true);
        }else{
            inforDto.setIsState("0");
        }
        inforDto.setCreateTime(DateUtils.getNowDate());
        inforDto.setAgentLevel("2");

        inforDto.setUserType("2");//1代表后管录入2小程序录入
        inforDto.setuId(user.getId());

        return aInformationMapper.insertAInformation(inforDto);
    }
}
